package 原子类;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2022-08-29 10:24
 **/


public class AtomicIntegerDemo {

    private static final int SIZE = 50;

    public static void main(String[] args) {
        final MyNumber myNumber = new MyNumber();
        final CountDownLatch countDownLatch = new CountDownLatch(SIZE);

        for (int i = 0; i < SIZE; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    myNumber.addPlusPlus();
                }
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(myNumber.getNumber());
    }

}

class MyNumber {

    private final AtomicInteger atomicInteger = new AtomicInteger();

    public void addPlusPlus() {
        atomicInteger.getAndIncrement();
    }

    public Integer getNumber() {
        return atomicInteger.get();
    }
}
